package com.nitty.cash.loop.ui.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.Paint
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.lib.common.adapter.CommonRecyclerViewAdapter
import com.lib.common.adapter.CommonViewHolderHelper
import com.nitty.cash.loop.NittyApp
import com.nitty.cash.loop.R
import com.nitty.cash.loop.weight.font.MediumButton
import org.json.JSONObject
import java.math.BigDecimal

/**
 * NittyCash
 * @Author wwdeng
 * @CreateTime: 2024/11/19 10:43
 * @Annotation：订单列表适配器
 */
class OrderListAdapter(recyclerView: RecyclerView?) : CommonRecyclerViewAdapter<JSONObject>(recyclerView) {

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        return when (model.optBoolean("isEmptyView",false)){
            true -> {
                R.layout.ui_order_list_empty
            }
            else -> {
                R.layout.ui_order_list_item
            }
        }
    }

    override fun setItemChildListener(helper: CommonViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.textWithdraw)
        helper.setItemChildClickListener(R.id.btnOrderDetails)
    }

    override fun fillData(helper: CommonViewHolderHelper, position: Int, model: JSONObject) {
        when (model.optBoolean("isEmptyView",false)){
            true -> {//空
                val textWithdraw = helper.getTextView(R.id.textWithdraw)
                textWithdraw.paint.flags = Paint.UNDERLINE_TEXT_FLAG//下划线
                textWithdraw.paint.isAntiAlias = true//抗锯齿
            }
            else -> {
                showNittyOrderView(helper,position,model)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun showNittyOrderView(helper: CommonViewHolderHelper, position: Int, model: JSONObject){
        val orderItemLayout = helper.getView<LinearLayout>(R.id.orderItemLayout)
        val imgOrderStatus = helper.getImageView(R.id.imgOrderStatus)
        val textOrderStatus = helper.getTextView(R.id.textOrderStatus)
        val textApplyAmount = helper.getTextView(R.id.textApplyAmount)
        val textApplyTime = helper.getTextView(R.id.textApplyTime)
        val btnOrderDetails = helper.getView<MediumButton>(R.id.btnOrderDetails)

        val loanAmount = BigDecimal(model.optString("loanAmount")).stripTrailingZeros().toPlainString()
        textApplyAmount.text = "$loanAmount ${model.optString("currency")}"
        textApplyTime.text = model.optString("applyDate")

        val orderStatus = model.optString("orderStatus")
        when(orderStatus){
            "IN_REVIEW" -> {//审核中
                orderItemLayout.setBackgroundResource(R.mipmap.nitty_order_item_reviewing_bg)
                imgOrderStatus.setImageResource(R.mipmap.nitty_order_item_reviewing_icon)
                textOrderStatus.text = NittyApp.app.getString(R.string.order_status_reviewing)
                textOrderStatus.setTextColor(Color.parseColor("#FFFFAE1A"))
                textApplyAmount.setTextColor(Color.parseColor("#FF4A403D"))
                textApplyTime.setTextColor(Color.parseColor("#FF4A403D"))
                btnOrderDetails.shapeDrawableBuilder
                    .setSolidColor(Color.parseColor("#FFFFAE1A"))
                    .intoBackground()
            }
            "LENDING" -> {//放款中
                orderItemLayout.setBackgroundResource(R.mipmap.nitty_order_item_disbursing_bg)
                imgOrderStatus.setImageResource(R.mipmap.nitty_order_item_disbursing_icon)
                textOrderStatus.text = NittyApp.app.getString(R.string.order_status_disbursing)
                textOrderStatus.setTextColor(Color.parseColor("#FF189EFD"))
                textApplyAmount.setTextColor(Color.parseColor("#FF4A403D"))
                textApplyTime.setTextColor(Color.parseColor("#FF4A403D"))
                btnOrderDetails.shapeDrawableBuilder
                    .setSolidColor(Color.parseColor("#FF52B4F8"))
                    .intoBackground()
            }
            "REJECT" -> {//已拒绝
                orderItemLayout.setBackgroundResource(R.mipmap.nitty_order_item_rejected_bg)
                imgOrderStatus.setImageResource(R.mipmap.nitty_order_item_rejected_icon)
                textOrderStatus.text = NittyApp.app.getString(R.string.order_status_rejected)
                textOrderStatus.setTextColor(Color.parseColor("#FFFD5118"))
                textApplyAmount.setTextColor(Color.parseColor("#FFFD5118"))
                textApplyTime.setTextColor(Color.parseColor("#FFFD5118"))
                btnOrderDetails.shapeDrawableBuilder
                    .setSolidColor(Color.parseColor("#FFFD5118"))
                    .intoBackground()
            }
        }
    }
}